import { useRoutes, Navigate } from "react-router-dom";
import { RouteObject } from "@/routes/interface";
import lazyLoad from "./utils/lazyLoad";
import React from "react";

//导入所有路由
const metaRouterArray = import.meta.glob("./modules/*.tsx", { eager: true });

//处理路由
export const routerArray: RouteObject[] = [];
Object.keys(metaRouterArray).forEach(item => {
	const moduleItem = metaRouterArray[item] as object;
	Object.keys(moduleItem).forEach(key => {
		//@ts-ignore
		routerArray.push(...moduleItem[key]);
	});
});

export const rootRouter: Array<RouteObject> = [
	{
		path: "/",
		element: <Navigate to={"/user/login"} />
	},
	{
		path: "/login",
		element: <Navigate to={"/user/login"} />
	},
	{
		path: "/user/login",
		element: lazyLoad(React.lazy(() => import("@/pages/user/Login/index"))),
		meta: {
			requiresAuth: false,
			title: "登录页",
			key: "login"
		}
	},
	...routerArray,
	{
		path: "*",
		element: <Navigate to={"/404"} />
	}
];

const Router = () => {
	//@ts-ignore
	return useRoutes(rootRouter);
};

export default Router;
